/**
 * long description for the file
 *
 * @summary 登录
 * @author wu[308822989@qq.com]
 *
 * Created at     : 2018-12-16 18:16:59
 * Last modified  : 2018-12-17 15:22:41
 */

module.exports = class extends think.Controller {
  async indexAction() {
    if (this.isPost) {
      let { code, pwd } = this.post();
      if (think.isEmpty(code) || think.isEmpty(pwd)) {
        return this.json({code: 1000, data: null, msg: '用户名密码不能为空'});
      } else {
        const modelAdmin = this.model('admin');
        let resUser = await modelAdmin.getUser(code);
        if (!think.isEmpty(resUser)) {
          if ((resUser.pwd + '') === pwd) {
            let token = think.uuid();
            think.cache(token, JSON.stringify(resUser));
            this.cookie('admin', token);
            return this.json({code: 0, data: token, msg: '登录成功'});
          } else {
            return this.json({code: 1000, data: null, msg: '密码错误'});
          }
        } else {
          return this.json({code: 1000, data: null, msg: '未找到用户'});
        }
      }
    }
    return this.display();
  }
};
